package com.yanqu.road.server.cmd.wingroom;

import com.yanqu.road.entity.wingroom.WingRoomRegion;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.wingroom.WingRoomModule;
import com.yanqu.road.server.pb.WingRoomPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2022/3/22 16:03
 */
@Cmd(code = GameProtocol.S_WING_ROOM_COPY_OTHER_PLAN_RESP, desc = "跨服拷贝方案返回")
public class CrossCopyPlanRespCmd extends AbstractCommand {
    private List<WingRoomProto.WingRoomRegionTemp> regionList;
    private WingRoomProto.GetOtherPlanRespMsg respMsg;
    private String planName;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        WingRoomProto.GetOtherPlanCrossRespMsg crossRespMsg = WingRoomProto.GetOtherPlanCrossRespMsg.parseFrom(packet.getBytes());
        regionList = crossRespMsg.getRegionList();
        respMsg = crossRespMsg.getRespMsg();
        planName = crossRespMsg.getPlanName();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        if (respMsg.getRet() != 0) {
            player.sendPacket(ClientProtocol.U_WING_ROOM_GET_OTHER_PLAN, respMsg.toBuilder());
            return;
        }
        Map<Integer, WingRoomRegion> planRegion = new ConcurrentHashMap<>();
        for (WingRoomProto.WingRoomRegionTemp wingRoomRegionTemp : regionList) {
            WingRoomRegion region = new WingRoomRegion();
            region.setRegionId(wingRoomRegionTemp.getRegionId());
            region.setFurnitureList(WingRoomPb.parseFurnitureList(wingRoomRegionTemp.getFurnitureList()));
            planRegion.put(region.getRegionId(), region);
        }
        WingRoomProto.GetOtherPlanRespMsg.Builder builder = respMsg.toBuilder();
        player.getModule(WingRoomModule.class).copyOtherPlayerPlan(planRegion, planName, builder);
        player.sendPacket(ClientProtocol.U_WING_ROOM_GET_OTHER_PLAN, builder);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
